﻿namespace Votations.NSurvey.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using Votations.NSurvey.IDAL;
    using Microsoft.ApplicationBlocks.Data;
    using System.Data;
    using System.Data.SqlClient;
    using Votations.NSurvey.Data;

    public class QuestionGroups : IQuestionGroup
    {

        public QuestionGroupsData GetAll(string langCode)
        {
            QuestionGroupsData groups = new QuestionGroupsData();
            SqlParameter[] spParameters = new SqlParameter[1];
            spParameters[0] = new SqlParameter("@LanguageCode", langCode);

            SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure,
                "vts_spQuestionGroupGetAll", groups, new string[] { "QuestionGroups" }, spParameters);

            return groups;
        }

        public QuestionGroupsData GetByQuestionId(int questionId)
        {
            QuestionGroupsData groups = new QuestionGroupsData();
            SqlParameter[] spParameters = new SqlParameter[1];
            spParameters[0] = new SqlParameter("QuestionId", questionId);

            SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spQuestionGroupGetByQuestionID", groups,
                new string[] { "QuestionGroups" }, spParameters);

            return groups;
        }

        public void AddNewGroup(string name, int? parentGroupId, string langId)
        {
            SqlParameter[] spParameters = new SqlParameter[4];
            spParameters[0] = new SqlParameter("@GroupName", name);
            spParameters[1] = new SqlParameter("@ParentGroupID", parentGroupId);
            spParameters[1].IsNullable = true;
            spParameters[2] = new SqlParameter("@LanguageCode", langId);
            spParameters[3] = new SqlParameter("@QuestionGroupID", SqlDbType.Int);
            spParameters[3].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spQuestionGroupAddNew", spParameters);
        }

        public void UpdateDisplayOrder(int questionGroupId, QuestionGroupDisplayOrder order)
        {
            SqlParameter[] spParameters = new SqlParameter[2];
            spParameters[0] = new SqlParameter("@QuestionGroupId", questionGroupId);
            spParameters[1] = new SqlParameter("@Up", (int)order);

            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spQuestionGroupUpdateDisplayID", spParameters);
        }


        public void UpdateGroup(int questionGroupId, int? parentGroupId, string name, string langId)
        {
            SqlParameter[] spParameters = new SqlParameter[4];
            spParameters[0] = new SqlParameter("@GroupName", name);
            spParameters[1] = new SqlParameter("@QuestionGroupID", questionGroupId);
            spParameters[2] = new SqlParameter("@ParentGroupID", parentGroupId);
            spParameters[3] = new SqlParameter("@LanguageCode", langId);

            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spQuestionGroupUpdate", spParameters);
        }


        public void DeleteGroup(int questionGroupId)
        {
            SqlParameter[] spParameters = new SqlParameter[1];
            spParameters[0] = new SqlParameter("@QuestionGroupID", questionGroupId);

            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, "vts_spQuestionGroupDelete", spParameters);
        }
    }
}
